package com.aerors.DataProcess;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureSource;
import org.geotools.data.Transaction;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFactory;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

public class TestGeoToolsUnion {
	private static final String shpPath = "C:\\data\\shapes\\TestUnion.shp";

	public static void main(String[] args) {
		Transaction transaction = new DefaultTransaction("update");
		DataStore dataStore = null;
		try {
			File file = new File(shpPath);
			Map<String, Object> map = new HashMap<>();
			map.put("url", file.toURI().toURL());
			dataStore = DataStoreFinder.getDataStore(map);
			String typeName = dataStore.getTypeNames()[0];
			FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);

			List<Geometry> list = new ArrayList<>();
			FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection = source.getFeatures();
			FeatureIterator<SimpleFeature> iterator = featureCollection.features();
			while (iterator.hasNext()) {
				SimpleFeature feature = iterator.next();

				Geometry geom = (Geometry) feature.getDefaultGeometry();
				list.add(geom);
			}

			Geometry union = combineIntoOneGeometry(list);
			SimpleFeatureStore featureStore = (SimpleFeatureStore) source;
			featureStore.setTransaction(transaction);

			SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(source.getSchema());
			featureBuilder.add(union);
			featureBuilder.add("union");
			featureBuilder.add("union");
			SimpleFeature feature = featureBuilder.buildFeature(null);

			List<SimpleFeature> features = new ArrayList<>();
			features.add(feature);
			SimpleFeatureCollection collection = new ListFeatureCollection(source.getSchema(), features);

			featureStore.addFeatures(collection);
			transaction.commit();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dataStore.dispose();
		}
	}

	static Geometry combineIntoOneGeometry(Collection<Geometry> geometryCollection) {
		GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null);

		GeometryCollection gCollection = (GeometryCollection) factory.buildGeometry(geometryCollection);

		return gCollection.union();
	}
}
